﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Create_User_2 : System.Web.UI.Page
{
    private bool fileSuccess = false;
    private string filePathName = "Images\\user_images\\no_profile.jpg";
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        imageUpload();
        
        SqlDataSource1.Insert();
    }

    private void imageUpload()
    {
        Boolean fileOK = false;
        String path = Server.MapPath("~/Images/user_images/");
        if (FileUpload1.HasFile)
        {
            String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {
            fileSuccess = true;
            try
            {
                filePathName = "Images\\user_images\\" + CreateUserWizard1.UserName + FileUpload1.FileName;
                FileUpload1.PostedFile.SaveAs(path + filePathName);
                FileUpload1.Visible = false;
            }
            catch (Exception ex)
            {
                fileSuccess = false;
                FileUpload1.Visible = false;
            }
        }
        else
        {
            FileUpload1.Visible = false;
        }



    }

    protected void On_Inserting_New_User(Object sender, SqlDataSourceCommandEventArgs e)
    {
        SqlParameter sp = new SqlParameter("currentUser", CreateUserWizard1.UserName);
        sp.Direction = ParameterDirection.Input;
        e.Command.Parameters.Add(sp);

        SqlParameter sp2 = new SqlParameter("url", filePathName);
        sp2.Direction = ParameterDirection.Input;
        e.Command.Parameters.Add(sp2);
    }
}
